package com.naiyumie.ncalculator;

import java.util.ArrayList;
import java.util.List;

import com.naiyumie.nlib.Nlog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener, OnClickListener {
	
	ListView lv1 = null;
	ArrayList<ListItem> list = null;
	DataAdapter adapter = null;
	Button aboutButton = null;
	Nlog nlog = new Nlog();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv1 = (ListView) findViewById(R.id.listView1);
		list = new ArrayList<ListItem>();
		adapter = new DataAdapter(this, list);
		aboutButton = (Button) findViewById(R.id.aboutButton);
		aboutButton.setOnClickListener(this);
		lv1.setAdapter(adapter);
		lv1.setOnItemClickListener(this);
		adapter.add(new ListItem("four_fundamental_rules01", getString(R.string.category01), getString(R.string.four_fundamental_rules01_title), R.drawable.icns_calc_four_fundamental_rules_of_arithmetics));
		adapter.add(new ListItem("percent01",                getString(R.string.category02), getString(R.string.percent01_title),                R.drawable.icns_calc_percent));
		adapter.add(new ListItem("percent02",                getString(R.string.category02), getString(R.string.percent02_title),                R.drawable.icns_calc_percent));
		adapter.add(new ListItem("percent03",                getString(R.string.category02), getString(R.string.percent03_title),                R.drawable.icns_calc_percent));
		adapter.add(new ListItem("percent04",                getString(R.string.category02), getString(R.string.percent04_title),                R.drawable.icns_calc_percent));
		adapter.add(new ListItem("percent_multiple01",       getString(R.string.category03), getString(R.string.percent_multiple01_title),       R.drawable.icns_calc_multy_percent));
		adapter.add(new ListItem("percent_multiple02",       getString(R.string.category03), getString(R.string.percent_multiple02_title),       R.drawable.icns_calc_multy_percent));
		adapter.add(new ListItem("stastics01",               getString(R.string.category04), getString(R.string.stastics01_title),               R.drawable.icns_calc_stastics));
	}
	
	public class DataAdapter extends ArrayAdapter<ListItem> {
		
		// list_item.xml을 열기위한 객체
		LayoutInflater inflater = null;
		
		// 생성자 선언
		public DataAdapter(Context context, List<ListItem> objects) {
			super(context, 0, objects);
			// LayoutInflater의 할당
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		// list_item.xml의 컴포넌트에 내용을 설정하는 메서드
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// list_item.xml을 담기위한 객체
			View view = null;
			if (convertView == null) {
				view = inflater.inflate(R.layout.list_item, null);
			} else {
				view = convertView;
			}
			// 전달된 데이터 얻기
			final ListItem item = getItem(position);
			final int index = position;
			if (item != null) {
				ImageView iv1 = (ImageView) view.findViewById(R.id.aboutBtn);
				iv1.setImageResource(item.getImg_id());
				TextView tv1 = (TextView) view.findViewById(R.id.tvHelperText);
				TextView tv2 = (TextView) view.findViewById(R.id.textView2);
				tv1.setText(item.getName());
				tv2.setText(item.getDesc());
			}
			return view;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.aboutButton:
				Intent intent = new Intent(this, AboutActivity.class);
				intent.putExtra("MSG", "about");
				startActivityForResult(intent, 100);
				break;
			default:
				break;
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ListItem item = adapter.getItem(arg2);
		String calId = item.getId();
		if (calId.equals(null) || calId.equals("")) {
			String msg = item.getName() + " / " + item.getDesc();
			Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
		} else {
			if (calId.equals("four_fundamental_rules01")) {
				Intent intent = new Intent(this, FourFundamentalRules01Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 101);
			} else if (calId.equals("percent01")) {
				Intent intent = new Intent(this, Percent01Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 110);
			} else if (calId.equals("percent02")) {
				Intent intent = new Intent(this, Percent02Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 111);
			} else if (calId.equals("percent03")) {
				Intent intent = new Intent(this, Percent03Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 112);
			} else if (calId.equals("percent04")) {
				Intent intent = new Intent(this, Percent04Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 113);
			} else if (calId.equals("percent_multiple01")) {
				Intent intent = new Intent(this, PercentMultiple01Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 114);
			} else if (calId.equals("percent_multiple02")) {
				Intent intent = new Intent(this, PercentMultiple02Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 115);
			} else if (calId.equals("stastics01")) {
				Intent intent = new Intent(this, Stastics01Activity.class);
				intent.putExtra("MSG", calId);
				startActivityForResult(intent, 115);
			} else {
				Toast.makeText(this, calId, Toast.LENGTH_SHORT).show();
			}
		}
	}
}
